<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */
 
namespace osc\mobile\controller;
use think\Db;
use think\Request;
use think\Session;
class Order extends MobileBase
{
	protected function _initialize(){
		parent::_initialize();						
		define('UID',is_login());	
		//手机版
		if(!UID){			
			$this->redirect('login/login');							
		}

		if (!Session::has("username")) {

            $this->redirect('login/login');

        }		
	}

	function test(){
		$this->error('暂未开放!');
	}

    /**
     * 元宝兑换
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
	function index(){
		$username = session('username');
		$user = Db::name('member')->where('username',$username)->find();
		$this->assign('jilin_bean',$user['jilin_bean']);
		$this->assign('jilin_bean2',$user['jilin_bean2']);
		$this->assign('status',(int)input('param.status'));
		$this->assign('top_title','元宝兑换');
		$this->assign('SEO',array('title'=>'元宝兑换-'.config('SITE_TITLE')));
		if(in_wechat()){
			$this->assign('signPackage',wechat()->getJsSign(request()->url(true)));	
		}
		return $this->fetch();
	}

    /**
     * 线上下元宝释放记录
     * @return mixed
     * @throws \think\exception\DbException
     */
	public function record(){
        $status=(int)input('param.status');
        $map['username'] = session('username');
        $map['type'] = 2;
        if ($status == 2 ){
            $map["DATE_FORMAT(FROM_UNIXTIME(create_time),'%Y-%m-%d')"] = ['>','2019-06-17'];
        }
        $list = Db::name('release')
            ->where($map)
            ->order('create_time desc')
            ->paginate(20,false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ])
            ->each(function($item, $key){
                $time = date('Y-m-d',$item['create_time']);
                if ($time > '2019-06-17'){
                    $status=(int)input('param.status');
                    if ($status == 1) {
                        $rom = 0.7;
                    }else{
                        $rom = 0.3;
                    }
                    $item['num'] = floor($item['num']*$rom*100)/100;
                    return $item;
                }else{
                    return $item;
                }
            });
//        print_r($list);exit;
        $page=$list->render(); //获取分页
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('empty','<br><h5 style="">暂无数据~~~</h5><br>');
        if ($status == 1){
            $this->assign('top_title','线上元宝释放记录');
        }else{
            $this->assign('top_title','线下元宝释放记录');
        }
        return $this->fetch();
    }

	/**
	 * 元宝专区
	 * @return mixed
	 */
	function imazamox(){
		$this->assign('status',(int)input('param.status'));
		$this->assign('top_title','商品兑换');
		$this->assign('SEO',array('title'=>'元宝专区-'.config('SITE_TITLE')));

		if(in_wechat()){
			$this->assign('signPackage',wechat()->getJsSign(request()->url(true)));
		}

		return $this->fetch();
	}


	/**
	 * 银豆专区
	 * @return mixed
	 */
	function silver(){
		$this->assign('status',(int)input('param.status'));
		$this->assign('top_title','银豆专区');
		$this->assign('SEO',array('title'=>'银豆专区-'.config('SITE_TITLE')));

		if(in_wechat()){
			$this->assign('signPackage',wechat()->getJsSign(request()->url(true)));
		}

		return $this->fetch();
	}

	/**
	 * 元宝订单
	 */
	function ajax_order_list(){
		
    	$page=(int)input('param.page');//页码

		$status=(int)input('param.status');
		//开始数字,数据量
		$limit = (5 * $page) . ",5";
		
		if(empty($status)){					
			$where=array('uid'=>UID);				
		}else{				
			$where=array('uid'=>UID,'order_status_id'=>$status);
		}

		$order=Db::name('order')->where($where)->order('order_id desc')->limit($limit)->select();

		$orders_list=array();
		
		//此处数据量大的时候会有性能问题，要考虑改写
		foreach ($order as $k => $v) {
						
			//普通商品或者积分商品
			if($v['type']==config('common_goods')||$v['type']==config('points_goods')){					
				$orders_list[$v['order_id']]['order']=$v;
				$orders_list[$v['order_id']]['list']=Db::view('OrderGoods','goods_id,name,order_goods_id,quantity,price,order_id')
				->view('Goods','image','Goods.goods_id=OrderGoods.goods_id')				
				->where('OrderGoods.order_id',$v['order_id'])->select();
			//团购商品
			}elseif($v['type']==config('tuan_goods')){				
				$orders_list[$v['order_id']]['order']=$v;
				$orders_list[$v['order_id']]['list']=Db::view('OrderGoods','goods_id,name,order_goods_id,quantity,price,order_id')
				->view('GroupBuy','group_image as image','OrderGoods.goods_id=GroupBuy.id')				
				->where('OrderGoods.order_id',$v['order_id'])->select();
				
			}
		}
		$this->assign('order',$orders_list);
		exit($this->fetch());
       
	}

	/**
	 * 银豆订单
	 */
	function ajax_silver_list(){

		$page=(int)input('param.page');//页码

		$status=(int)input('param.status');
		//开始数字,数据量
		$limit = (5 * $page) . ",5";

		if(empty($status)){
			$where=array('uid'=>UID);
		}else{
			$where=array('uid'=>UID,'order_status_id'=>$status);
		}
		$order=Db::name('second_order')->where($where)->order('order_status_id asc,order_id desc')->limit($limit)->select();

		$orders_list=array();

		//此处数据量大的时候会有性能问题，要考虑改写
		foreach ($order as $k => $v) {

			//普通商品或者积分商品
			if($v['type']==config('common_goods')||$v['type']==config('points_goods')){
				$orders_list[$v['order_id']]['order']=$v;
				$orders_list[$v['order_id']]['list']=Db::view('SecondGoods','goods_id,name,order_goods_id,quantity,price,order_id')
						->view('Goods','image','Goods.goods_id=SecondGoods.goods_id')
						->where('SecondGoods.order_id',$v['order_id'])
						->select();
				//团购商品
			}elseif($v['type']==config('tuan_goods')){
				$orders_list[$v['order_id']]['order']=$v;
				$orders_list[$v['order_id']]['list']=Db::view('SecondGoods','goods_id,name,order_goods_id,quantity,price,order_id')
						->view('GroupBuy','group_image as image','SecondGoods.goods_id=GroupBuy.id')
						->where('SecondGoods.order_id',$v['order_id'])
						->select();
			}
		}
		$this->assign('order',$orders_list);
		exit($this->fetch());

	}
	
	function order_info(){
		if(!$order=osc_order()->order_info(input('param.order_id'),UID)){
			$this->error('请勿重复提交！！');
		}
		cookie('order_info_url',request()->url(true));
		$this->assign('order',$order);	
		$this->assign('SEO',array('title'=>'订单详情-'.config('SITE_TITLE')));
		$this->assign('top_title','订单详情');
		return $this->fetch();
	}

	function silver_info(){
		if(!$order=osc_order()->silver_info(input('param.order_id'),UID)){
			$this->error('请勿重复提交！！');
		}
		$cart = [];
		if($order['order']['cart_id'] != 0){
			$cart = Db::name('cart')->where(['cart_id'=>$order['order']['cart_id']])->find();
		}
//		print_r($cart);exit;
		$this->assign('cart',$cart);
		cookie('order_info_url',request()->url(true));
		$this->assign('order',$order);
		$this->assign('SEO',array('title'=>'订单详情-'.config('SITE_TITLE')));
		$this->assign('top_title','订单详情');
		return $this->fetch();
	}

	function cancel_order(){
		osc_order()->cancel_order((int)input('param.order_id'),UID);
		storage_user_action(UID,member('nickname'),config('FRONTEND_USER'),'取消了订单');
		return 1;
	}
	//物流信息
	function get_trace_info(){
		
		return osc_service('common','Kuaidi')->get_trace_info(input('param.order_id'),'mobile');
				
	}
	//元宝订单确认收货
	function confirm(){
		$id=input('param.order_id');		
						
		osc_order()->confirm_order($id,UID);
		
		storage_user_action(UID,member('username'),config('FRONTEND_USER'),'确认收货');
		
		return 1;
	}

	//银豆订单确认收货
	function silver_confirm(){
		$id=input('param.order_id');

		osc_order()->silver_confirm_order($id,UID);

		storage_user_action(UID,member('username'),config('FRONTEND_USER'),'确认收货');

		return 1;
	}
}
